package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin //解决跨域问题
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/demo")
    @ResponseBody
    public String Demo(){
        return "SSM";
    }

    /**
     * 查询所有的用户列表数据，在userList.html中展现数据
     */
    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userList",userList);
        return "userList";
    }

    /**
     * 需求：利用restFul实现用户数据插入，之后重定向到userList.html页面
     * @param user
     * @return
     * 优化策略：
     *      如果有多个参数提交，则可以使用对象接收，但是要求参数名必须与属性名称一致
     */
    @RequestMapping("/user/{name}/{age}/{sex}")
    public String insertUser(User user){
        userService.insertUser(user);
        return "redirect:/userList";
    }

    @RequestMapping("/userAjax")
    public String userAjax(){
        return "userAjax";
    }



    @GetMapping("/findAll")
    @ResponseBody
    public List<User> findAll(){
        return userService.findAll();
    }

    @PostMapping("addUser")
    @ResponseBody
    public void addUser(@RequestBody User user){
        userService.addUser(user);
    }

    @PutMapping("/updateUser")
    @ResponseBody
    public void updateUser(@RequestBody User user){
        userService.updateUser(user);
    }

    @DeleteMapping("deleteUser")
    @ResponseBody
    public void deleteUser(Integer id){
        userService.deleteUser(id);
    }
	
	@RequestMapping("/userVue")
	public String userVue(){
	    return "userVue";
	}

}
